Gikam.extend(limsReportTemplate.editPage,{

    getGridParam : function() {
        var _this = this;
        var baseGridToolbar = _this.getBaseGridToolbar();
        var gridToolbar = _this.appendGridToolbar(baseGridToolbar);
        gridToolbar.push({
            type : 'button',
            text : 'LIMS.MODULE.REPORTTEMPLATES.BUTTON.VIEW_PDF',
            icon : 'preview',
            onClick : function() {
                _this.createWord(limsReportTemplate.baseUrl + '/{id}/action/generator/pdf');
            }
        });
        return {
            type : 'grid',
            id : _this.idManager.limsReportTemplateEditGrid,
            url : limsReportTemplate.baseUrl + '/queries/raw',
            columns : _this.getEditListColumns(),
            service : 'limsReportTemplateService',
            confirmField : 'templateName',
            onCellClick : function(field, row) {
                field === 'templateFileName' && Gikam.openFile(row.templateFileId, false);
            },
            onLoadSuccess : function(data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    Gikam.cleanCompData([_this.idManager.limsReportTemplateTemplateSourceGrid]);
                    Gikam.getAsyncComp(_this.idManager.limsReportTemplateTemplateFileUploader).done(function(uploader) {
                        uploader.setOptions({
                            bizId : null
                        })
                        uploader.grid.cleanData();
                    })
                }
            },
            onRowActive : function(index, row) {
                Gikam.getAsyncComp(_this.idManager.limsReportTemplateTemplateSourceGrid).done(function(grid) {
                    grid.refresh({
                        url : Gikam.printf(limsReportTemplate.baseUrl + '/{id}/template-sources/queries', {
                            id : row.id
                        })
                    });
                });

                Gikam.getAsyncComp(_this.idManager.limsReportTemplateTemplateFileUploader).done(function (uploader) {
                    uploader.setOptions({
                        bizId : row.id
                    });
                });
            },
            toolbar : gridToolbar
        }
    },

    createWord : function(templateUrl) {
        var _this = this;
        var templateGrid = Gikam.getComp(_this.idManager.limsReportTemplateEditGrid);
        var template = templateGrid.getActivedRow();
        if (Gikam.isEmpty(template)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_ONE_ITEM');
            return;
        }
        if (Gikam.isEmpty(template.param)) {
            var url = Gikam.printf(templateUrl, {
                id : template.id
            });
            workspace.window.showMask();
            Gikam.postText(url).done(function(path) {
                workspace.window.closeMask();
                window.open(window.location.origin + path);
                // Gikam.printFile(fileId);
            }).always(function() {
                workspace.window.closeMask();
            });
        } else {
            var paramArray = template.param.split(',');
            var fields = paramArray.map(function(value) {
                return {
                    field : value,
                    title : value,
                }
            });
            var height = 110 + 40 * paramArray.length;
            var simpleModal = Gikam.createSimpleModal({
                title : 'LIMS.MODULE.WORDTEMPLATES.TAB.INPUT_PARAM',
                width : 250,
                height : height,
                showMaxBtn : false,
                bodyStyle : {
                    padding : "0 16px"
                },
                toolbarFormatter : function() {
                    return [{
                        type : 'button',
                        text : 'GIKAM.BUTTON.CONFIRM',
                        icon : 'select',
                        onClick : function() {
                            if (Gikam.isFalse(form.validate())) {
                                return;
                            }
                            var url = Gikam.printf(templateUrl, {
                                id : template.id
                            });
                            Gikam.getLastModal().window.showMask();
                            Gikam.postText(url, Gikam.getJsonWrapper({
                                f : form.getData()
                            })).done(function(path) {
                                Gikam.getLastModal().close();
                                window.open(window.location.origin + path);
                                // Gikam.printFile(fileId);
                            }).always(function() {
                                Gikam.getLastModal() && Gikam.getLastModal().window.closeMask();
                            })
                        }
                    }];
                }
            });

            var form = Gikam.create('form', {
                columns : 1,
                titleWidth : 80,
                fields : fields,
                renderTo : simpleModal.layout.options.center.$dom
            });
        }
    },

    getEditListColumns : function() {
        return [{
            checkbox : true
        }, {
            field : 'templateCode',
            title : 'T_LIMS_REPORT_TEMPLATE.TEMPLATECODE',
            width : 100,
        }, {
            field : 'templateName',
            title : 'T_LIMS_REPORT_TEMPLATE.TEMPLATENAME',
            editor : true,
            type :  'richText',
            validators : ['notEmpty', 'strLength(0,200)'],
            width : 100,
        }, {
            field : 'param',
            title : 'T_LIMS_REPORT_TEMPLATE.PARAM',
            editor : true,
            type :  'richText',
            validators : ['strLength(0, 200)'],
            width : 100,
        }, {
            field : 'templateType',
            title : 'T_LIMS_REPORT_TEMPLATE.TEMPLATETYPE',
            type : 'select',
            category : 'sys',
            validators : ['notEmpty'],
            firstBlank : false,
            editor : true,
            param : {
                codeCategoryId : 'templateType'
            }
        }, {
            field : 'templateFileName',
            title : 'T_LIMS_REPORT_TEMPLATE.TEMPLATEFILENAME',
            type : 'link',
            width : 150
        }, {
            field : 'option',
            title : 'T_LIMS_REPORT_TEMPLATE.OPTION',
            contentAlign : 'center',
            formatter : function(index, value, row) {
                var fileFlag = Gikam.isNotEmpty(row.templateFileId);
                return Gikam.printf('<a href="javascript:;" onclick="limsReportTemplate.editPage.uploadTemplateFile(\'{id}\', \'{index}\', \'{dbTable}\')">{name}</a>&nbsp;&nbsp;<a href="javascript:;" onclick="Gikam.reportTemplateDownload(\'{fileId}\')">{download}</a>', {
                    id : row.id,
                    index : index,
                    dbTable : 'T_LIMS_REPORT_TEMPLATE',
                    fileId : row.templateFileId,
                    name : fileFlag ? Gikam.propI18N('LIMS.MODULE.REPORTTEMPLATES.BUTTON.UPDATE') : Gikam.propI18N('LIMS.MODULE.REPORTTEMPLATES.BUTTON.UPLOADER'),
                    download : Gikam.propI18N('LIMS.BASEMODULE.BUTTON.FILE_DOWNLOAD'),
                });
            },
            width : 200
        }, {
            field : 'remark',
            title : 'T_LIMS_REPORT_TEMPLATE.REMARK',
            type : 'richText',
            editor : true,
            validators : ['notEmpty','strLength(0, 300)']
        }];
    },

    getTemplateSourceGrid : function() {
        var _this = this;
        return {
            title : 'LIMS.MODULE.REPORTTEMPLATES.TAB.TEMPLATE_SOURCE',
            items : [{
                type : 'grid',
                id : _this.idManager.limsReportTemplateTemplateSourceGrid,
                service : 'limsReportTemplateSourceServiceImpl',
                confirmField : 'sourceCode',
                onCellClick : function(field, row) {
                    field === 'sourceCode' && _this.showSourceDetail(row);
                },
                toolbar : [{
                    type : 'button',
                    text : 'GIKAM.BUTTON.INSERT',
                    icon : 'add-row',
                    onClick : function() {
                        var activatedRow = Gikam.getComp(_this.idManager.limsReportTemplateEditGrid).getActivedRow();
                        if (Gikam.isEmpty(activatedRow)) {
                            return;
                        }
                        Gikam.preInsert({
                            modalTitle : 'GIKAM.BUTTON.INSERT',
                            fields : limsReportTemplate.getTemplateSourceFormFields(),
                            url : Gikam.printf(limsReportTemplate.baseUrl + '/{id}/template-sources', {
                                id : activatedRow.id
                            })
                        }).done(function(id) {
                            Gikam.getComp(_this.idManager.limsReportTemplateTemplateSourceGrid).refresh();
                            _this.showSourceDetail({
                                id : id
                            })
                        });
                    }
                }, {
                    type : 'button',
                    text : 'GIKAM.BUTTON.DELETE',
                    icon : 'remove-row',
                    onClick : function() {
                        var activatedRow = Gikam.getComp(_this.idManager.limsReportTemplateEditGrid).getActivedRow();
                        if (Gikam.isEmpty(activatedRow)) {
                            return;
                        }
                        Gikam.getComp(_this.idManager.limsReportTemplateTemplateSourceGrid).deleteRows(Gikam.printf(limsReportTemplate.baseUrl + '/{id}/template-sources', {
                            id : activatedRow.id
                        }));
                    }
                }],
                columns : _this.getTemplateSourceListColumns()
            }]
        };
    },

    getTemplateSourceListColumns : function() {
        return [{
            checkbox : true
        }, {
            field : 'sourceCode',
            validators : ['notEmpty'],
            title : 'T_LIMS_REPORT_TEMPLATE_SOURCE.SOURCECODE',
            type : 'link'
        }, {
            field : 'sourceTable',
            title : 'T_LIMS_REPORT_TEMPLATE_SOURCE.SOURCETABLE',
            validators : ['notEmpty']
        }, {
            field : 'sourceName',
            title : 'T_LIMS_REPORT_TEMPLATE_SOURCE.SOURCENAME',
            validators : ['notEmpty']
        }, {
            field : 'sourceType',
            title : 'T_LIMS_REPORT_TEMPLATE_SOURCE.SOURCETYPE',
            type : 'select',
            category : 'sys',
            param : {
                codeCategoryId : 'templateSourceType'
            },
            validators : ['notEmpty']
        }, {
            field : 'param',
            title : 'T_LIMS_REPORT_TEMPLATE_SOURCE.PARAM',
        }, {
            field : 'parentSourceCode',
            title : 'T_LIMS_REPORT_TEMPLATE_SOURCE.PARENTSOURCECODE',
            editor : true,
            validators : ['strLength(0,30]']
        }, {
            field : 'remark',
            title : 'T_LIMS_REPORT_TEMPLATE_SOURCE.REMARK',
            type : 'richText'
        }];
    },
});